import Home from "@/pages/home/index.jsx";
import { treeMap } from "../utils/util.js";
import { isEmpty, isExist } from "wsp-toolkit";

import {
  FileSearchOutlined,
  HomeOutlined,
  SearchOutlined,
  SettingOutlined,
} from "@ant-design/icons";

export const mainRoutes = [
  {
    index: true,
    element: <Home />,
    meta: {
      title: "首页",
      icon: <HomeOutlined />,
    },
  },
  {
    path: "search",
    meta: {
      title: "查询管理",
      icon: <SearchOutlined />,
    },
    children: [
      {
        path: "credit-report",
        lazy: () => import("@/pages/search/credit-report"),
        meta: {
          title: "征信查询记录",
        },
      },
      {
        path: "credit-report/report-detail",
        lazy: () =>
          import(
            "@/pages/search/credit-report/ReportDetail/report-detail.jsx"
          ),
        meta: {
          title: "征信查询记录详情",
          isShowMenu: false,
        },
      },
    ],
  },

  {
    path: "log",
    meta: {
      title: "日志查询",
      icon: <FileSearchOutlined />,
    },
    children: [
      {
        path: "login",
        lazy: () => import("@/pages/log-record/login-log"),
        meta: {
          title: "登录日志",
        },
      },
    ],
  },
  {
    path: "setting",
    meta: {
      title: "系统管理",
      icon: <SettingOutlined />,
    },
    children: [
      {
        path: "user",
        lazy: () => import("@/pages/setting/user"),
        meta: {
          title: "用户管理",
        },
      },
      {
        path: "roles",
        lazy: () => import("@/pages/setting/roles"),
        meta: {
          title: "角色管理",
        },
      },
    ],
  },
  {
    path: "error",
    children: [
      {
        path: "access-denied",
        lazy: () => import("@/pages/error/access-denied"),
      },
    ],
  },
];

export const menuItems = treeMap(
  mainRoutes,
  (route, parent) => {
    if (isEmpty(route.meta?.title) && isEmpty(route.children)) return null;
    return {
      icon: route.meta?.icon,
      label: route.meta?.title,
      key: route.indexPath,
      path: route.path,
      meta: route.meta
    };
  },
  {
    beforeFn: (route, parent, i) => {
      /* if(isExist(route.meta?.isShowMenu) && !route.meta?.isShowMenu){
        parent.children = void 0;
        return null;
      }; */
      return {
        ...route,
        path: [parent?.path || "", route.path || ""].join("/"),
        indexPath: isExist(parent?.indexPath)
          ? `${parent.indexPath}-${i}`
          : `${i}`,
      };
    },
  },
);
